package com.yuuzy.view;

import com.yuuzy.dao.Buy_InsuranceDAO;
import com.yuuzy.model.Buy_Insurance;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

public class VieworderInsurance extends JInternalFrame {
    private JTable insurance_table;

    private Buy_InsuranceDAO buy_insuranceDAO = new Buy_InsuranceDAO();
    private JTextField insurance_idtext;
    private JTextField client_idText;

    /**
     * Create the frame.
     */
    public VieworderInsurance() {
        setClosable(true);
        setIconifiable(true);
        setTitle("查看保险订单");
        setBounds(100, 100, 760, 726);

        JScrollPane scrollPane = new JScrollPane();

        JLabel lblNewLabel = new JLabel("保险编号：");

        JLabel lblNewLabel_1 = new JLabel("客户编号：");

        insurance_idtext = new JTextField();
        insurance_idtext.setColumns(10);

        client_idText = new JTextField();
        client_idText.setColumns(10);

        JButton btnNewButton = new JButton("查询 ");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                queryActionPerformed(e);
            }
        });
        btnNewButton.setIcon(new ImageIcon(VieworderInsurance.class.getResource("/image/pm-query-16.png")));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.TRAILING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(74)
                                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 582, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(133)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addComponent(lblNewLabel_1)
                                                                .addGap(16)
                                                                .addComponent(client_idText, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addComponent(lblNewLabel)
                                                                .addGap(17)
                                                                .addComponent(insurance_idtext, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)))))
                                .addContainerGap(88, Short.MAX_VALUE))
                        .addGroup(groupLayout.createSequentialGroup()
                                .addContainerGap(439, Short.MAX_VALUE)
                                .addComponent(btnNewButton)
                                .addGap(182))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.TRAILING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(49)
                                                .addComponent(lblNewLabel)
                                                .addGap(46)
                                                .addComponent(lblNewLabel_1))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(47)
                                                .addComponent(insurance_idtext, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addGap(5)
                                                .addComponent(btnNewButton)
                                                .addGap(5)
                                                .addComponent(client_idText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 413, GroupLayout.PREFERRED_SIZE)
                                .addGap(90))
        );

        insurance_table = new JTable();
        insurance_table.setFillsViewportHeight(true);
        insurance_table.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "保险编号", "客户id", "购买时间", "保险状态"
                }
        ));
        insurance_table.getColumnModel().getColumn(0).setPreferredWidth(125);
        insurance_table.getColumnModel().getColumn(1).setPreferredWidth(100);
        insurance_table.getColumnModel().getColumn(2).setPreferredWidth(176);
        insurance_table.getColumnModel().getColumn(3).setPreferredWidth(245);
        scrollPane.setViewportView(insurance_table);
        getContentPane().setLayout(groupLayout);


        this.fillTable(new Buy_Insurance());
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    VieworderInsurance frame = new VieworderInsurance();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 查询事件处理
     *
     * @param evt
     */
    private void queryActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String insurance_id = this.insurance_idtext.getText();
        String client_id = this.client_idText.getText();
        Buy_Insurance buy_insurance = new Buy_Insurance(Integer.parseInt(insurance_id), Integer.parseInt(client_id));
        this.fillTable(buy_insurance);
    }

    /**
     * 初始化表格
     *
     * @param buy_insurance
     */
    private void fillTable(Buy_Insurance buy_insurance) {
        DefaultTableModel dtm = (DefaultTableModel) insurance_table.getModel();
        dtm.setRowCount(0);//设置为0行，清空表格
        List<Buy_Insurance> orderList = buy_insuranceDAO.queryBuy_Insurance(buy_insurance);
        for (Buy_Insurance rs : orderList) {
            Vector v = new Vector();
            v.add(rs.getInsurance_id());
            v.add(rs.getClient_id());
            v.add(rs.getBuy_insurance_time());
            v.add(rs.getBuy_insurance_assetstatus());
            dtm.addRow(v);
        }
    }
}
